package com.tvsinfo.jdk.demo.case6_higher_order_function.order;

import lombok.val;

import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class OrderQueryStarter {


    public static void main(String[] args) {

    }

    public void buildPageQueryParameter(OrderPageRequest request) {
        //订单状态 转换后新key集合
        Collection<Integer> newStatuss = new ArrayList<>();
        Collection<Integer> oldStatuss = request.getInOrderStatuses();
        for (Integer t : oldStatuss) {
            List<OrderStatusEnum> tempNewKeys = OrderStatusEnum.getByOldKey(t);
            for (OrderStatusEnum statusEnum : tempNewKeys) {
                newStatuss.add(statusEnum.getKey());
            }
        }
        //设置新keys集合
        request.setInOrderStatuses(newStatuss);


        //订单类型
        Collection<Integer> newTypes = new ArrayList<>();
        Collection<Integer> oldTypes = request.getInOrderTypes();
        for (Integer t : oldTypes) {
            List<OrderTypeEnum> tempNewKeys = OrderTypeEnum.getByOldType(t);
            for (OrderTypeEnum typeEnum : tempNewKeys) {
                newTypes.add(typeEnum.getKey());
            }
        }
        request.setInOrderTypes(newTypes);


        //维权类型
        Collection<Integer> newRefunds = new ArrayList<>();
        Collection<Integer> oldRefunds = request.getInRefundStatuses();
        for (Integer t : oldRefunds) {
            //有新的对接方和老的对接方，老的对接方传老值，新的对接方传新值，但是不会存在新老值混用的情况
            if (!(t > 1 && t < 100)) {
                continue;
            }
            List<RefundStatusEnum> tempNewKeys = RefundStatusEnum.getByOldKey(t);
            for (RefundStatusEnum typeEnum : tempNewKeys) {
                newRefunds.add(typeEnum.getKey());
            }
        }
        request.setInRefundStatuses(newRefunds);





        //订单状态OrderStatusEnum
        request.setInOrderStatuses(switchEnum(
                request.getInOrderStatuses(),
                OrderStatusEnum::getByOldKey,
                OrderStatusEnum::getKey));

        //订单类型枚举
        request.setInOrderTypes(switchEnum(
                request.getInOrderTypes(),
                OrderTypeEnum::getByOldType,
                OrderTypeEnum::getKey));

        //退款状态枚举
        request.setInRefundStatuses(switchEnum(
                filter(request.getInPayStatuses(), integer -> integer > 1 && integer < 100),
                RefundStatusEnum::getByOldKey,
                RefundStatusEnum::getKey));

        //支付状态枚举
        request.setInPayStatuses(switchEnum(
                filter(request.getInPayStatuses(), integer -> integer <= 1 || integer > 100),
                PayStatusEnum::getByOldKey,
                PayStatusEnum::getKey));
    }




    /**
     * 根据条件过滤
     */
    private Collection<Integer> filter(
            Collection<Integer> list,
            Predicate<Integer> predicate) {
        if (null == list) {
            return null;
        }
        return list.stream()
                .filter(predicate)
                .collect(Collectors.toList());
    }


    /**
     * 用于转换数据
     * create by Ruyi.Song on 2018/8/16 16:17
     *
     * @param oldKeys     需要转换的集合
     * @param toEnumsList 表达式
     * @param enumToNew   转换使用的表达式
     * @return 返回转换后的集合
     */
    public static <Enum> Collection<Integer> switchEnum(
            Collection<Integer> oldKeys,
            Function<Integer, List<Enum>> toEnumsList,
            Function<Enum, Integer> enumToNew) {

        if (null == oldKeys || oldKeys.isEmpty()) {
            return null;
        }

        Collection<Integer> newKeys = new HashSet<>();

        for (Integer integer : oldKeys) {
            //说明外部已经转换过订单类型
            if (integer > 100) {
                newKeys.add(integer);
                continue;
            }
            //换到的新结合
            Collection<Enum> apply = toEnumsList.apply(integer);
            if (null == apply) {
                continue;
            }
            val currentTempNewKeys =
                    apply.stream()
                            .map(enumToNew)
                            .collect(Collectors.toCollection(ArrayList::new));

            newKeys.addAll(currentTempNewKeys);
        }
        return newKeys;
    }

}
